package leetcode.method.sliding_window;

public class Program209OverTargetMinSubArrayLen {
    public Program209OverTargetMinSubArrayLen() {
    }
    public int minSubArrayLen(int target, int[] nums) {
        if (nums.length == 0) {
            return 0;
        }
        if (nums[0] >= target) {
            return 1;
        }
        int sum = nums[0];
        int i = 0, j = 1;
        int minLength = nums.length + 1;
        while (j < nums.length) {
            sum += nums[j];
            if (sum >= target) {
                while (sum >= target) {
                    if (sum - nums[i] < target) {
                        break;
                    }
                    sum -= nums[i];
                    i++;
                }
                minLength = Math.min(minLength, j - i + 1);
                sum -= nums[i];
                i++;
            }
            j++;
        }
        return minLength == nums.length + 1 ? 0 : minLength;
    }

    public static void main(String[] args) {
        Program209OverTargetMinSubArrayLen myStack = new Program209OverTargetMinSubArrayLen();
        int[] nums = new int[]{2,3,1,2,4,3};
        int k = 7;
        System.out.println(myStack.minSubArrayLen(k, nums)); // 返回
    }
}
